<?php
namespace Admin\Controller;
use Lib\Controller\BaseController;
use Think\Upload;

class HospitalController extends BaseController{

    private $ProvinceModel;    //省份
    private $CityModel;        //城市
    private $HospitalModel;    //医院
    private $videoBookModel;   //预定订单
    private $videoBookInfoModel;
    private $errVideoModel;


    public function __construct(){
        $this->videoBookInfoModel = D('videoBookInfo');
        $this->videoBookModel   = D('VideoBook');
        $this->ProvinceModel    = D('Province');
        $this->CityModel        = D('City');
        $this->HospitalModel    = D('Hospital');
        $this->errVideoModel    = D('errVideo');
        parent::__construct();
    }
    
    /**
     * 双AB摄像头 的APP更新接口
     */
    public function version_to_upload(){
        $data['version'] = '1.0.2';
        $data['update_package'] = 'http://babyvideo.zhfancon.com/upload/app/1.0.2.apk';
        
        $this->returnjson(1,'当前版本号为'.$data['version'],$data);
    }
    /**
     * ABC摄像头 的APP更新接口
     */
    public function version_to_upload_abc(){
        $data['version'] = '1.0.1';
        $data['update_package'] = 'http://babyvideo.zhfancon.com/upload/app/abc_carema.apk';
    
        $this->returnjson(1,'当前版本号为'.$data['version'],$data);
    }
    /**
     * 单独C摄像头 的APP更新接口
     */
    public function version_to_upload_c(){
        $data['version'] = '1.0.2';
        $data['update_package'] = 'http://babyvideo.zhfancon.com/upload/app/one_carema.apk';
    
        $this->returnjson(1,'当前版本号为'.$data['version'],$data);
    }

    public function index(){
        $city     = $this->CityModel->select();
        $province = $this->ProvinceModel->select();
        $hospital = $this->HospitalModel->select();

        $this->assign('city',$city);
        $this->assign('province',$province);
        $this->assign('hospital',$hospital);
        $this->display();
    }

    /**
     * [saveVideoOrder 保存预定订单--后台或医院方人员上传]
     * @author xiezy
     * @date   2018-01-11
     * @return [type]     [description]
     */
    public function saveVideoOrder(){
        $saveData 		= I('post.');
        $ordersn        = date('YmdHis') . str_pad(mt_rand(1, 999999999), 9, '0', STR_PAD_LEFT);  //生成订单号
        // $saveData['user_id']        = -1;           // -1 为管理人员
        $saveData['ordersn'] 		= $ordersn;    	//订单号
        $saveData['create_time'] 	= time();    	//创建时间

        $priceWhere['id']           = $saveData['hospitalID'];
        $saveData['amount']         = $this->HospitalModel->where($priceWhere)->getField('hospital_price');
        //保存订单内容
        $this->videoBookModel->add($saveData);

        //保存订单详情内容
        $saveInfoData['ordersn']     = $ordersn;
        $saveInfoData['bron_time']   = strtotime($saveData['bron_time']);
        $saveInfoData['baby_height'] = $saveData['baby_height'];
        $saveInfoData['baby_weight'] = $saveData['baby_weight'];
        $this->videoBookInfoModel->add($saveInfoData);
        //上传图片的配置
        $config = array(
            'maxSize' => 500*1024*1024, //上传的文件大小限制 (0-不做限制)
            'exts' => array('mp4', 'flv', 'avi','png','jpg'),// 设置附件上传类型
            'subName' => array('date', 'Ymd'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
            'saveName'   =>    array('uniqid',''),
            'savePath' => C('UPLOAD_VIDEO_PATH'), //保存路径
            'rootPath' => './Upload/',
            'autoSub'    =>    true,
        );
        $this->uploader = new Upload($config, 'Local');
        $info = $this->uploader->upload($_FILES);
        if(!$info){//提示错误信息
            $returndata ['status'] = 0; // 状态
            $returndata ['info'] = $this->uploader->getError();
            $this->ajaxReturn($returndata);
        }else{
            //最终视频路径
            foreach($info as $x =>$val){
                $saveBookInfoImgDate['material_video_'.$x] = $val['savepath'] . $val['savename'];
            }
            $where['ordersn'] = $ordersn;
            $uploadResult = $this->videoBookInfoModel->where($where)->save($saveBookInfoImgDate);
            if($uploadResult!==false){
                $returndata['status'] = 1;
                $returndata['info']   = '上传成功';
                $returndata['url']    = U(MODULE_NAME.'/hospital/index');
                $this->ajaxReturn($returndata);
            }else{
                $returndata['status'] = 0;
                $returndata['info']   = '上传失败';
                $this->ajaxReturn($returndata);
            }
        }
    }


    public function empty_function(){
        $returndata['status'] = 1;
        $this->ajaxReturn($returndata);
    }

    /**
     * [getProvince 获取省份]
     * @author xiezy
     * @date   2018-03-30
     * @return [type]     [description]
     */
    public function getProvince(){
    	$province = $this->ProvinceModel->select();
    	return $this->returnjson(1,'获取成功',$province);
    }

    /**
     * [getCityByProvince 获取城市、区县]
     * @author xiezy
     * @date   2018-01-10
     * @return [type]     [description]
     */
    public function getCityByProvince(){
    	$proVal 			 = I('post.p_id');
    	$where['ProvinceID'] = $proVal;
    	$city     			 = $this->CityModel->where($where)->select();

    	return $this->returnjson(1,'获取成功',$city);
    }

    /**
     * [getCityByProvince 获取城市、区县]
     * @author xiezy
     * @date   2018-01-10
     * @return [type]     [description]
     */
    public function getHospitalByCity(){
        $where['city_id']     = I('post.c_id');
        $where['status']      = 1;
        $hospital             = $this->HospitalModel->field('id,hospital_name')->where($where)->select();

    	return $this->returnjson(1,'获取成功',$hospital);
    }


    /**
     * [upload_babyvideo 上传宝宝视频]
     */
    public function upload_babyvideo(){
        $file_name = $_FILES['upload_babyvideo_0']['name'];
        $v_num = 1;
        if($_FILES['upload_babyvideo_1']['name'] != ''){
            $v_num = 2;
        }
        if($_FILES['upload_babyvideo_2']['name'] != ''){
            $v_num = 3;
        }
        if($_FILES['upload_babyvideo_3']['name'] != ''){
            $v_num = 4;
        }
        if($_FILES['upload_babyvideo_4']['name'] != ''){
            $v_num = 5;
        }
        if($_FILES['upload_babyvideo_5']['name'] != ''){
            $v_num = 6;
        }

        $file_name_arr = explode('_',$file_name);

        $where['hospitalNO'] = $file_name_arr[1];
        $where['hospitalID'] = $file_name_arr[0];
        $vbOrder = 1;//$this->videoBookModel->where($where)->order('id desc')->find();
        if($vbOrder){
            $check = $this->videoBookModel->where($where)->order('id desc')->find();
            if($check){
                $checkinfo = $this->videoBookInfoModel->where('ordersn ='.$check['ordersn'])->find();
                $saveVideoPath = C('UPLOAD_VIDEO_PATH').$check['ordersn'].'/';
            }else{
                $saveVideoPath = 'err_video/';
            }
            
            //上传图片的配置
            $config = array(
                'maxSize' => 0, //上传的文件大小限制 (0-不做限制)
                'exts' => array('mp4', 'flv', 'avi','png','jpg'),// 设置附件上传类型
                'subName' => array('date', 'Ymd'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                'saveName'   =>    array('uniqid',''),
                'savePath' => $saveVideoPath, //保存路径
                'rootPath' => './Upload/',
                'autoSub'    =>    true,
            );
            $this->uploader = new Upload($config, 'Local');
            $info = $this->uploader->upload($_FILES);
            if(!$info){//提示错误信息
                $returndata ['status'] = 0; // 状态
                $returndata ['info'] = $this->uploader->getError();
                return $this->returnjson(500,$this->uploader->getError());
            }else{
                //最终视频路径
                for($x=0;$x<=$v_num-1;$x++){
                    $saveBookInfoImgDate['material_video_'.$x] = $info['upload_babyvideo_'.$x]['savepath'] . $info['upload_babyvideo_'.$x]['savename'];
                    $errorData['material_video_'.$x] = $info['upload_babyvideo_'.$x]['savepath'] . $info['upload_babyvideo_'.$x]['savename'];
                }
//                 p($check);p($checkinfo);
                //判断是否会被占用视频路径
                if($check && $checkinfo['material_video_0'] == ''){
                    $vbiwhere['ordersn'] = $check['ordersn'];
                    $uploadResult = $this->videoBookInfoModel->where($vbiwhere)->save($saveBookInfoImgDate);
                }else{
                    $errorData['hospital_id'] = $file_name_arr[0];
                    $errorData['hospital_no'] = $file_name_arr[1];
                    $errorData['upload_time'] = time();
                    $uploadResult = $this->errVideoModel->add($errorData);
                }
                
                if($uploadResult!==false){
                    $returndata['status'] = 1;
                    $returndata['info']   = '上传成功';
                    return $this->returnjson(1,'上传成功');
                }else{
                    $returndata['status'] = 0;
                    $returndata['info']   = '上传失败';
                    return $this->returnjson(500,'上传失败');
                }
            }
        }else{
            $returndata['status'] = 0;
            $returndata['info']   = '查无订单';
            return $this->returnjson(0,'查无订单');
        }

    }
    
    /**
     * [upload_babyvideo 上传宝宝视频]
     */
    public function upload_babyvideo_c(){
        $file_name = $_FILES['upload_babyvideo_c']['name'];
        $v_num = 1;
//         if($_FILES['upload_babyvideo_1']['name'] != ''){
//             $v_num = 2;
//         }
//         if($_FILES['upload_babyvideo_2']['name'] != ''){
//             $v_num = 3;
//         }
//         if($_FILES['upload_babyvideo_3']['name'] != ''){
//             $v_num = 4;
//         }
    
        $file_name_arr = explode('_',$file_name);
    
        $where['hospitalNO'] = $file_name_arr[1];
        $where['hospitalID'] = $file_name_arr[0];
        $vbOrder = 1;//$this->videoBookModel->where($where)->order('id desc')->find();
        if($vbOrder){
            $check = $this->videoBookModel->where($where)->order('id desc')->find();
            if($check){
                $checkinfo = $this->videoBookInfoModel->where('ordersn ='.$check['ordersn'])->find();
                $saveVideoPath = C('UPLOAD_VIDEO_PATH').$check['ordersn'].'/';
            }else{
                $saveVideoPath = 'err_video/';
            }
    
            //上传图片的配置
            $config = array(
                'maxSize' => 0, //上传的文件大小限制 (0-不做限制)
                'exts' => array('mp4', 'flv', 'avi','png','jpg'),// 设置附件上传类型
                'subName' => array('date', 'Ymd'), //子目录创建方式，[0]-函数名，[1]-参数，多个参数使用数组
                'saveName'   =>    array('uniqid',''),
                'savePath' => $saveVideoPath, //保存路径
                'rootPath' => './Upload/',
                'autoSub'    =>    true,
            );
            $this->uploader = new Upload($config, 'Local');
            $info = $this->uploader->upload($_FILES);
            if(!$info){//提示错误信息
                $returndata ['status'] = 0; // 状态
                $returndata ['info'] = $this->uploader->getError();
                return $this->returnjson(500,$this->uploader->getError());
            }else{
                //最终视频路径
                for($x=0;$x<=$v_num-1;$x++){
                    $saveBookInfoImgDate['material_video_c'] = $info['upload_babyvideo_c']['savepath'] . $info['upload_babyvideo_c']['savename'];
                    $errorData['material_video_c'] = $info['upload_babyvideo_c']['savepath'] . $info['upload_babyvideo_c']['savename'];
                }
                //                 p($check);p($checkinfo);
                //判断是否会被占用视频路径
                if($check && $checkinfo['material_video_c'] == ''){
                    $vbiwhere['ordersn'] = $check['ordersn'];
                    $uploadResult = $this->videoBookInfoModel->where($vbiwhere)->save($saveBookInfoImgDate);
                }else{
                    $errorData['hospital_id'] = $file_name_arr[0];
                    $errorData['hospital_no'] = $file_name_arr[1];
                    $errorData['upload_time'] = time();
                    $uploadResult = $this->errVideoModel->add($errorData);
                }
    
                if($uploadResult!==false){
                    $returndata['status'] = 1;
                    $returndata['info']   = '上传成功';
                    return $this->returnjson(1,'上传成功');
                }else{
                    $returndata['status'] = 0;
                    $returndata['info']   = '上传失败';
                    return $this->returnjson(0,'上传失败');
                }
            }
        }else{
            $returndata['status'] = 0;
            $returndata['info']   = '查无订单';
            return $this->returnjson(0,'查无订单');
        }
    
    }

    public function logo_img(){
        $result = D('config')->where('config_name = "img_url"')->find();

        return $this->returnjson(1,'上传成功',$result);
    }


    /**
     * json方式输出通信消息
     * @param int $code
     * @param string $message
     * @param array $data
     */
    function returnjson($code,$message = '',$data = array()){
        if(!is_numeric($code)){
            return false;
        }

        $result = array(
            'code'=>$code,
            'message'=>$message,
            'data'=>$data
        );

        echo json_encode($result);
    }
}
?>
